//
//  LocationHandler.m
//  EarnMoneyPlatform
//
//  Created by 航汇聚科技 on 2018/7/16.
//  Copyright © 2018年 Yi Zhou. All rights reserved.
//

#import "LocationHandler.h"

@interface LocationHandler ()

@property (nonatomic, copy) NSString *tempLocationInfo;
@property (nonatomic, strong) NSMutableDictionary *tempLocationAllInfo;

@end

@implementation LocationHandler

+ (instancetype)defaultLocationHandler {
    static LocationHandler *instance = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        instance = [LocationHandler new];
    });
    return instance;
}

- (void)requestLocationAuthorization {
    self.locationManager.delegate = self;
    [_locationManager requestWhenInUseAuthorization];
    self.locationManager.distanceFilter = 500;
    [self startUpdatingLocation];
}

- (void)startUpdatingLocation {
    self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];
    self.locationManager.distanceFilter = 500;
}

- (BOOL)isAuthLocation {
    BOOL result = NO;
    switch ([CLLocationManager authorizationStatus]) {
        case kCLAuthorizationStatusAuthorizedAlways:
        case kCLAuthorizationStatusAuthorizedWhenInUse:
        {
            result = YES;
        }
            break;
        default:
            break;
    }
    return result;
}

#pragma mark CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    
    CLGeocoder *geocoder = [CLGeocoder new];
    
    [geocoder reverseGeocodeLocation:[locations lastObject] completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        CLPlacemark *mark = [placemarks lastObject];
        
        NSLog(@"Name: %@",mark.name);
        NSLog(@"Thoroughtfare: %@",mark.thoroughfare);
        NSLog(@"SubThoroughfare: %@",mark.subThoroughfare);
        NSLog(@"locality: %@",mark.locality);
        
        NSLog(@"subLocality: %@",mark.subLocality);
        
        NSLog(@"ISOcountryCode: %@",mark.ISOcountryCode);
        
        self.tempLocationAllInfo = [@{
                                      locationLatitude:mark.location==nil?@"":[NSString stringWithFormat:@"%f",mark.location.coordinate.latitude],
                                      locationLongitude:mark.location==nil?@"":[NSString stringWithFormat:@"%f",mark.location.coordinate.longitude],
                                      locationName:mark.name==nil?@"":mark.name,
                                      locationThoroughfare:mark.thoroughfare==nil?@"":mark.thoroughfare,
                                      locationSubThoroughfare:mark.subThoroughfare==nil?@"":mark.subThoroughfare,
                                      locationLocality:mark.locality==nil?@"":mark.locality,
                                      locationSubLocality:mark.subLocality==nil?@"":mark.subLocality,
                                      locationISOcountryCode:mark.ISOcountryCode==nil?@"":mark.ISOcountryCode
                                      } mutableCopy];
        
//        NSLog(@"AddressDictionary: %@",mark.addressDictionary);
        
        self.tempLocationInfo = mark.name;
        
        [[NSNotificationCenter defaultCenter] postNotificationName:NOTFICATION_LOCATIONALLINFO object:nil userInfo:self.tempLocationAllInfo];
    }];
    
    [self.locationManager stopUpdatingLocation];
}

#pragma mark getter
- (NSString *)locationInfo {
    return [self.tempLocationInfo copy];
}
- (NSDictionary *)locationAllInfo {
    return [self.tempLocationAllInfo copy];
}
- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
    }
    return _locationManager;
}
- (NSMutableDictionary *)tempLocationAllInfo {
    if (!_tempLocationAllInfo) {
        _tempLocationAllInfo = [@{locationName:@"",
                                  locationThoroughfare:@"",
                                  locationSubThoroughfare:@"",
                                  locationLocality:@"",
                                  locationSubLocality:@"",
                                  locationISOcountryCode:@""} mutableCopy];
    }
    return _tempLocationAllInfo;
}
@end
